package com.ruoyi.web.zangchat.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.zangchat.domain.ChatComments;
import com.ruoyi.web.zangchat.service.IChatCommentsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动态评论Controller
 * 
 * @author wudi
 * @date 2020-07-22
 */
@Controller
@RequestMapping("/zangchat/comments")
public class ChatCommentsController extends BaseController
{
    private String prefix = "zangchat/comments";

    @Autowired
    private IChatCommentsService chatCommentsService;

    @RequiresPermissions("zangchat:comments:view")
    @GetMapping()
    public String comments()
    {
        return prefix + "/comments";
    }

    /**
     * 查询动态评论列表
     */
    @RequiresPermissions("zangchat:comments:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChatComments chatComments)
    {
        startPage();
//        return getDataTable(chatCommentsService.list(new QueryWrapper<ChatComments>(chatComments)));
        return getDataTable(chatCommentsService.chatCommentsList(chatComments));
    }

    /**
     * 导出动态评论列表
     */
    @RequiresPermissions("zangchat:comments:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChatComments chatComments)
    {
        List<ChatComments> list = chatCommentsService.list(new QueryWrapper<>());
        ExcelUtil<ChatComments> util = new ExcelUtil<ChatComments>(ChatComments.class);
        return util.exportExcel(list, "comments");
    }

    /**
     * 新增动态评论
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存动态评论
     */
    @RequiresPermissions("zangchat:comments:add")
    @Log(title = "动态评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChatComments chatComments)
    {
        return toAjax(chatCommentsService.save(chatComments));
    }

    /**
     * 修改动态评论
     */
    @GetMapping("/edit/{commentId}")
    public String edit(@PathVariable("commentId") Long commentId, ModelMap mmap)
    {
        ChatComments chatComments = chatCommentsService.getById(commentId);
        mmap.put("chatComments", chatComments);
        return prefix + "/edit";
    }

    /**
     * 修改保存动态评论
     */
    @RequiresPermissions("zangchat:comments:edit")
    @Log(title = "动态评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChatComments chatComments)
    {
        return toAjax(chatCommentsService.updateById(chatComments));
    }

    /**
     * 删除动态评论
     */
    @RequiresPermissions("zangchat:comments:remove")
    @Log(title = "动态评论", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chatCommentsService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
